<template>
  <div class="notifications-container">
    <el-dialog
      v-model="visible"
      title="申请通知"
      width="500px"
      :before-close="closeDialog"
    >
      <div class="notifications-list">
        <div 
          v-for="notification in notifications" 
          :key="notification.id"
          class="notification-item"
          :class="notification.type"
        >
          <div class="notification-header">
            <span class="notification-title">{{ notification.title }}</span>
            <span class="notification-time">{{ formatTime(notification.time) }}</span>
          </div>
          <div class="notification-content">
            {{ notification.content }}
          </div>
          <div class="notification-actions" v-if="notification.actions">
            <el-button 
              v-for="action in notification.actions"
              :key="action.key"
              :type="action.type"
              size="small"
              @click="handleAction(action, notification)"
            >
              {{ action.text }}
            </el-button>
          </div>
        </div>
        
        <div v-if="notifications.length === 0" class="empty-notifications">
          <p>暂无通知</p>
        </div>
      </div>
      
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="markAllAsRead" v-if="notifications.length > 0">
            全部标记为已读
          </el-button>
          <el-button type="primary" @click="closeDialog">
            关闭
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'

export default {
  name: 'ApplicationNotifications',
  props: {
    modelValue: {
      type: Boolean,
      default: false
    }
  },
  emits: ['update:modelValue'],
  setup(props, { emit }) {
    const notifications = ref([
      {
        id: 1,
        type: 'success',
        title: '申请提交成功',
        content: '您向腾讯科技提交的前端开发工程师申请已成功提交，请等待企业回复。',
        time: new Date(Date.now() - 2 * 60 * 60 * 1000), // 2小时前
        read: false,
        actions: [
          {
            key: 'view',
            text: '查看详情',
            type: 'primary'
          }
        ]
      },
      {
        id: 2,
        type: 'info',
        title: '申请状态更新',
        content: '您向阿里巴巴提交的Java后端工程师申请正在审核中，请耐心等待。',
        time: new Date(Date.now() - 24 * 60 * 60 * 1000), // 1天前
        read: true,
        actions: [
          {
            key: 'view',
            text: '查看详情',
            type: 'primary'
          }
        ]
      },
      {
        id: 3,
        type: 'success',
        title: '申请被接受',
        content: '恭喜！您向字节跳动提交的算法工程师申请已被接受，企业将联系您进行下一步沟通。',
        time: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000), // 3天前
        read: false,
        actions: [
          {
            key: 'contact',
            text: '联系企业',
            type: 'success'
          },
          {
            key: 'view',
            text: '查看详情',
            type: 'primary'
          }
        ]
      }
    ])

    const visible = computed({
      get: () => props.modelValue,
      set: (value) => emit('update:modelValue', value)
    })

    const unreadCount = computed(() => {
      return notifications.value.filter(n => !n.read).length
    })

    const formatTime = (time) => {
      const now = new Date()
      const diff = now - time
      
      if (diff < 60 * 1000) {
        return '刚刚'
      } else if (diff < 60 * 60 * 1000) {
        return `${Math.floor(diff / (60 * 1000))}分钟前`
      } else if (diff < 24 * 60 * 60 * 1000) {
        return `${Math.floor(diff / (60 * 60 * 1000))}小时前`
      } else {
        return `${Math.floor(diff / (24 * 60 * 60 * 1000))}天前`
      }
    }

    const closeDialog = () => {
      visible.value = false
    }

    const markAllAsRead = () => {
      notifications.value.forEach(notification => {
        notification.read = true
      })
      ElMessage.success('已全部标记为已读')
    }

    const handleAction = (action, notification) => {
      switch (action.key) {
        case 'view':
          ElMessage.info('查看申请详情')
          break
        case 'contact':
          ElMessage.success('正在为您联系企业')
          break
        default:
          break
      }
      
      // 标记为已读
      notification.read = true
    }

    const addNotification = (notification) => {
      notifications.value.unshift({
        id: Date.now(),
        read: false,
        time: new Date(),
        ...notification
      })
    }

    return {
      notifications,
      visible,
      unreadCount,
      formatTime,
      closeDialog,
      markAllAsRead,
      handleAction,
      addNotification
    }
  }
}
</script>

<style scoped>
.notifications-container {
  /* 容器样式 */
}

.notifications-list {
  max-height: 400px;
  overflow-y: auto;
}

.notification-item {
  padding: 16px;
  border-radius: 8px;
  margin-bottom: 12px;
  border-left: 4px solid #e1e5e9;
  background: #f8f9fa;
  transition: all 0.3s ease;
}

.notification-item:hover {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.notification-item.success {
  border-left-color: #67c23a;
  background: #f0f9ff;
}

.notification-item.info {
  border-left-color: #409eff;
  background: #f0f9ff;
}

.notification-item.warning {
  border-left-color: #e6a23c;
  background: #fdf6ec;
}

.notification-item.error {
  border-left-color: #f56c6c;
  background: #fef0f0;
}

.notification-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.notification-title {
  font-size: 14px;
  font-weight: bold;
  color: #333;
}

.notification-time {
  font-size: 12px;
  color: #666;
}

.notification-content {
  font-size: 13px;
  color: #666;
  line-height: 1.5;
  margin-bottom: 12px;
}

.notification-actions {
  display: flex;
  gap: 8px;
}

.empty-notifications {
  text-align: center;
  padding: 40px 0;
  color: #666;
}

.dialog-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .notification-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
  
  .notification-actions {
    flex-direction: column;
    gap: 8px;
  }
  
  .dialog-footer {
    flex-direction: column;
    gap: 12px;
  }
}
</style> 